package com.example.mongo.controller;

import com.example.mongo.Repository.PersonRepository;
import com.example.mongo.entity.Location;
import com.example.mongo.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @创建者：zzw
 * @创建时间 2018/7/31 0031
 * @描述
 */
@RestController
public class PersonController {

    @Autowired
    PersonRepository personRepository;

    @RequestMapping("/save")
    public Person save(){
        Person p = new Person("zzw",27);
        Collection<Location> locations = new LinkedHashSet<>();
        Location loc1 = new Location("上海", "2011");
        Location loc2 = new Location("北京", "2012");
        Location loc3 = new Location("深圳", "2013");
        Location loc4 = new Location("南昌", "2018");
        locations.add(loc1);
        locations.add(loc2);
        locations.add(loc3);
        locations.add(loc4);
        p.setLocations(locations);
        return personRepository.save(p);
    }

    @RequestMapping("/findByName")
    public Person findByName(String name){
        return personRepository.findByName(name);
    }

    @RequestMapping("/findByAge")
    public List<Person> findByAge(Integer age){
        return  personRepository.withQueryFindByAge(age);
    }
}
